local shangyi = fk.CreateSkill {

  name = "joy__shangyi",

  tags = {  },

}



shangyi:addEffect("active", {
  name = "joy__shangyi",
  anim_type = "control",
  card_num = 1,
  target_num = 1,
  prompt = "#joy__shangyi",
  can_use = function(self, player)
    return player:usedSkillTimes(shangyi.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 and not player:prohibitDiscard(Fk:getCardById(to_select)) then
      if Fk:currentRoom():getCardArea(to_select) == Player.Hand then
        return player:getHandcardNum() > 1
      else
        return true
      end
    end
  end,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player.id and not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:throwCard(effect.cards, shangyi.name, player, player)
    if player.dead or target.dead or player:isKongcheng() or target:isKongcheng() then return end
    U.viewCards(target, player:getCardIds("h"), shangyi.name)
    local id = room:askForCardChosen(player, target, { card_data = { { "$Hand", target:getCardIds(Player.Hand) }  } }, shangyi.name,"尚义：选择其中一张牌，你获得之或与自己的一张手牌交换")
    local choice1 = "获得此牌，若此牌为黑你摸一张牌"
    local choice2 = "交换此牌，若此牌为红你摸一张牌"
    local choices = {choice1}
    if not player:isKongcheng() then
      table.insertIfNeed(choices,choice2)
    end
    local choice = room:askForChoice(player,choices,shangyi.name,"尚义：选择一项执行",false,{choice1,choice2})
    if choice == choice1 then
      room:obtainCard(player, id, false, fk.ReasonPrey)
      if Fk:getCardById(id).color == Card.Black and not player.dead then
        player:drawCards(1,shangyi.name)
      end
    elseif choice == choice2 then
      target:showCards({id})
      local card = room:askForCard(player,1,1,false,shangyi.name,false,".","选择一张手牌与 "..Fk:translate(target.general).." 的牌交换。")
      if #card > 0 then
        local move1 = {
          from = player.id,
          ids = {card[1]},
          to = target.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player.id,
          skillName = shangyi.name,
        }
        local move2 = {
          from = target.id,
          ids ={id},
          to = player.id,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player.id,
          skillName = shangyi.name,
        }
        room:moveCards(move1, move2)
        if not player.dead and Fk:getCardById(id).color == Card.Red then
          player:drawCards(1,shangyi.name)
        end
      end
    end
  end,
})

return shangyi